package com.example.a1111;


import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class ExpenseStatsActivity extends AppCompatActivity {

    private TextView totalAmountTextView;
    private ListView expensesListView;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expense_stats);

        totalAmountTextView = findViewById(R.id.totalAmountTextView);
        expensesListView = findViewById(R.id.expensesListView);

        dbHelper = new DBHelper(this);

        displayTotalAmount();
        displayExpenses();
    }

    @SuppressLint("SetTextI18n")
    private void displayTotalAmount() {
        double totalAmount = dbHelper.getTotalAmount();
        totalAmountTextView.setText("Total Amount: $" + totalAmount);
    }

    private void displayExpenses() {
        Cursor cursor = dbHelper.getAllExpenses();
        String[] fromColumns = {DBHelper.COLUMN_AMOUNT, DBHelper.COLUMN_DATE};
        int[] toViews = {R.id.amountTextView, R.id.dateTextView};
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.expense_item, cursor, fromColumns, toViews, 0);
        expensesListView.setAdapter(adapter);
    }
}
